<?php

namespace App\Http\Controllers\Admin;

use App\Models\AdminUser;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{
    /**
     *  登录
     */
    public function login()
    {
        if(Session::has('uid')){
            return redirect('admin/');
        }
        return view('admin.login.login');
    }

    public function loginPost(Request $request)
    {
        $pwd = md5(md5($request->input('password')));
        $info = AdminUser::where('name',$request->input('name'))->first();
        if($info){
            if($info->password != $pwd){
                return response()->json(['code'=>0,'msg'=>'密码错误']);
            }
            Session::forget('uid');
            Session::put('uid',$info->id);

            return response()->json(['code'=>1,'msg'=>'登录成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'账号错误']);
        }
    }

    /**
     *  登出
     */
    public function logout()
    {
        if(Session::has('uid')){
            Session::forget('uid');
            return redirect('admin/auth/login');
        }else{
            return redirect('admin/');
        }
    }

}
